﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using Itegracion.Framework.Forms;

namespace Itegracion.iAlmacen.Forms
{
    public partial class ucDocumento : ucEdicion
    {
        public ucDocumento()
        {
            InitializeComponent();
        }

        private void tbDocId_Leave(object sender, EventArgs e)
        {
            tbNombre.Clear();
            tbDireccion.Clear();
            _("Itegracion.iAlmacen", "Persona", "TraerByDocId", tbDocId)
                .Out(tbNombre, tbDireccion);
        }

        private void ucDocumento_Load(object sender, EventArgs e)
        {
            dgvItems.AutoGenerateColumns = false;
        }

        private void dgvItems_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            Calcular(e.RowIndex);
        }
        private void dgvItems_CellValidated(object sender, DataGridViewCellEventArgs e)
        {
            Calcular(e.RowIndex);
        }

        private void Calcular(int rowIndex)
        {
            decimal c = Convert.ToDecimal(dgvItems.Rows[rowIndex].Cells["cantidad"].Value);
            decimal pu = Convert.ToDecimal(dgvItems.Rows[rowIndex].Cells["precio_unitario"].Value);
            dgvItems.Rows[rowIndex].Cells["SubTotal"].Value = c * pu;

            decimal st = 0;
            foreach (DataGridViewRow item in dgvItems.Rows)
            {
                st += Convert.ToDecimal(item.Cells["SubTotal"].Value);
            }
            tbSubTotal.Text = string.Format("{0:N2}", st);
            tbIGV.Text = string.Format("{0:N2}", st * 18/100);
            tbTotal.Text = string.Format("{0:N2}", st * 118 / 100);
        }

        private void dgvItems_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                e.SuppressKeyPress = true;
        }
    }
}
